<?php

include( "./admin/include/func.php");
include( "Pager/Pager.php");

$db = new db();
if ( ! $db->connect()) exit( mysql_error());

$g_id			 = isset( $_GET['sorszam']) ? (int) $_GET['sorszam'] : 0; 
$g_list          = (isset ($_GET['list']) && in_array ($_GET['list'],array ("kiemelt","friss","osszes","lejaro","ismerkedes"))) ? $_GET['list'] : '';
$g_felado		 = isset( $_GET['felado']) ? (int) $_GET['felado'] : 0; 

$g_szoveg        = isset( $_GET['szoveg']) ? strip_tags( $_GET['szoveg']) : '';       
$g_szovegben     = isset( $_GET['szovegben']);
$g_targyban      = isset( $_GET['targyban']);
$g_nevben        = isset( $_GET['nevben']);
$g_telepulesben  = isset( $_GET['telepulesben']);
$g_emailben      = isset( $_GET['emailben']);
$g_weblapban     = isset( $_GET['weblapban']);
$g_sorszamban    = isset( $_GET['sorszamban']);
$g_rovat         = isset( $_GET['rovat']) ? strip_tags( $_GET['rovat']) : 'mindegy';
$g_regio         = isset( $_GET['regio']) ? strip_tags( $_GET['regio']) : 'mindegy';
$g_minar         = (isset( $_GET['minar']) && $_GET['minar'] != '') ? max( 0, (int) $_GET['minar']) : '';
$g_maxar         = (isset( $_GET['maxar']) && $_GET['minar'] != '') ? min( (int) $_GET['maxar'], 999999999) : '';

$g_showform  = isset( $_GET['showform']);
$g_showtotal = isset( $_GET['showtotal']);
$g_showback  = isset( $_GET['showback']);

if (! $g_showform) {
	
	$filter = '1=1';

	if ($g_list != '') {

		switch ($g_list) {
			case "kiemelt":
				$filter .= " AND szponzoralt>0 AND rovat!=50 AND rovat!=51";
			break;
			case "friss":
				$filter .= " AND ((lastmodified>=$ma AND lastmodified<$holnap) OR szponzoralt>0) AND rovat!=50 AND rovat!=51";
			break;
			case "osszes":
				$filter .= " AND rovat!=50 AND rovat!=51";
			break;
			case "lejaro":
				$filter .= " AND DATEDIFF(lejarat,$ma)=0 AND rovat!=50 AND rovat!=51";
			break;
			case "ismerkedes":
				$filter .= " AND rovat=50 OR rovat=51";
			break;
		}
	}

    
    $plus_or_filter = 0;
    
    $or_filter = '';
    
	if ($g_szovegben)    {$or_filter .= " OR szoveg LIKE '{$g_szoveg}%'";     $plus_or_filter = 1;}

	if ($g_targyban)     {$or_filter .= " OR targy LIKE '{$g_szoveg}%'";      $plus_or_filter = 1;}

	if ($g_nevben)       {$or_filter .= " OR nev LIKE '{$g_szoveg}%'";        $plus_or_filter = 1;} 

	if ($g_telepulesben) {$or_filter .= " OR  telepules LIKE '{$g_szoveg}%'"; $plus_or_filter = 1;} 

	if ($g_emailben)     {$or_filter .= " OR  email LIKE '{$g_szoveg}%'";     $plus_or_filter = 1;} 

	if ($g_weblapban)    {$or_filter .= " OR  weblap LIKE '{$g_szoveg}%'";    $plus_or_filter = 1;} 
							
	if ($g_sorszamban)   {$or_filter .= " OR  azonosito LIKE '{$g_szoveg}%'"; $plus_or_filter = 1;}
	
	if ($plus_or_filter > 0) $filter .= " AND (1=2 $or_filter)";
	
	
	if ($g_felado) $filter .= " AND email=(SELECT h.email FROM hirdetes h WHERE h.id='$g_felado' LIMIT 1)";      
		 
	if ($g_rovat != "mindegy" && $g_rovat != '0') $filter .= " AND (rovat=(SELECT r.id FROM rovat r WHERE r.slug='$g_rovat' LIMIT 1) OR rovat IN (SELECT r.id FROM rovat r WHERE r.parent=(SELECT s.id FROM rovat s WHERE s.slug='$g_rovat' LIMIT 1)))";

	if ($g_regio != "mindegy") $filter .= " AND regio LIKE '%{$g_regio}%'";

	if ($g_minar != "") $filter .= " AND ar>=$g_minar";

	if ($g_maxar != "") $filter .= " AND ar<=$g_maxar";

	//default filter
	if ($filter == '1=1') $filter .= " AND rovat!=50 AND rovat!=51";
	
	//ha meg van adva az id akkor csak arra keresunk
	if ($g_id > 0) $filter = "id='$g_id'";

	$db->sql = "SELECT COUNT(id)
				FROM hirdetes 
				WHERE $filter AND aktiv=1";			
	$db->query();

	$rpp = 15; //row per page
	$tct = mysql_fetch_row($db->rs);
	$tct = $tct[0];	//total count
	$pager_options = array('mode' => 'Sliding', 'perPage' => $rpp, 'delta' => 2, 'totalItems' => $tct,);
	$pager = @Pager::factory($pager_options);
	list($from, $to) = $pager->getOffsetByPageId();

	$db->sql = "SELECT id,nev,email,targy,szoveg,ar,telefon,weblap,kep,telepules,feladas,lejarat,aktivkod,szponzoralt,
				(SELECT r.parent FROM rovat r WHERE r.id=rovat) as rovat_parent,
				(SELECT r.id FROM rovat r WHERE r.id=rovat) as rovat_id1, 
				(SELECT r.id FROM rovat r WHERE r.id=rovat_parent) as rovat_id2,
				(SELECT IF(rovat_parent=0,rovat_id1,rovat_id2)) as forovat_id,
				(SELECT IF(rovat_parent=0,0,rovat_id1)) as alrovat_id,
				(SELECT r.nev  FROM rovat r WHERE r.id=forovat_id) as forovat,
				(SELECT r.slug FROM rovat r WHERE r.id=forovat_id) as forovat_slug,
				(SELECT r.nev  FROM rovat r WHERE r.id=alrovat_id) as alrovat,
				(SELECT r.slug FROM rovat r WHERE r.id=alrovat_id) as alrovat_slug,
				(SELECT r.nev  FROM regio r WHERE r.id=regio) as regio  
				FROM hirdetes h
				WHERE $filter AND aktiv=1
				ORDER BY szponzoralt DESC, lastmodified DESC
				LIMIT " . ($from - 1) . ", $rpp";				
	$db->query();
	//print $db->sql;
	
	$hirdetesek = array ();
	while ($row = mysql_fetch_array( $db->rs)) $hirdetesek[] = $row;
	//debug( $hirdetesek);
}

?>

<?php include ("header.php"); ?>

<?php include ("top.php"); ?>

<?php include ("left.php"); ?>
					
<div id="middle">

<div id="hirbox">Betöltés...</div>

<?php if ($g_showform) { ?>

	<div class="form-panel">		
		<div>
			<img src='./images1/x3a2.gif' width="21px" height="42px" class="fleft" alt='apróhirdetés' /> 
			<div class="hptop">
				<p class="hpcaption">Keresés</p>
				<p class="hptitle">Adja meg a keresési feltételeket!</p>
			</div>	
			<img src='./images1/x3b2.gif' width="21px" height="42px" class="fleft" alt='apróhirdetés' />
		</div>
		<div>
			<img src='./images1/x4a.gif' width="21px" height="13px" class="fleft" alt='apróhirdetés' /> 
			<img src='./images1/x4bg.gif' width="455px" height="13px" class="fleft" alt='apróhirdetés' /> 
			<img src='./images1/x4b.gif' width="21px" height="13px" class="fleft" alt='apróhirdetés' />
		</div>
		<div>
			<img src='./images1/x5bga.gif' width="21px" height="250px" class="fleft" alt='apróhirdetés' /> 
			<div class="hpbody" style="height:250px">
				<form name="form_kereses" id="form_kereses" method="get" enctype='application/x-www-form-urlencoded' accept-charset="utf-8">										

					<input name='showtotal' type='hidden' value="1" />
					
					<label for='szoveg'>Keresett szöveg</label>
					<input name='szoveg' type='text' value="<?php print $g_szoveg; ?>" />
					<br />

					<label for='mezok'>Keresési mezők</label>
					<div id="keresesi-mezok">
						<p><input name='szovegben' type='checkbox' <?php if ($g_szovegben) print "checked='true'"; ?> />Szöveg</p>
						<p><input name='targyban' type='checkbox' <?php if ($g_targyban) print "checked='true'"; ?> />Tárgy</p>
						<p><input name='nevben' type='checkbox' <?php if ($g_nevben) print "checked='true'"; ?> />Feladó</p>
						<p><input name='telepulesben' type='checkbox' <?php if ($g_telepulesben) print "checked='true'"; ?> />Település</p>
						<p><input name='emailben' type='checkbox' <?php if ($g_emailben) print "checked='true'"; ?> />Email</p>
						<p><input name='weblapban' type='checkbox' <?php if ($g_weblapban) print "checked='true'"; ?> />Weblap</p>
						<p><input name='sorszamban' type='checkbox' <?php if ($g_sorszamban) print "checked='true'"; ?> />Sorszám</p>
					</div>
					<br />
					
					<label for='rovat'>Rovat</label>
					<select name='rovat'>  
						<?php
							if (count($rovatok) < 1) print "<option value='0'>Create a Rovat first!</option>";
							else {
								print "<option value='mindegy' " . ($g_rovat == $rovat['slug'] ? "selected='selected'" : '') . ">Mindegy</option>";
								foreach ($rovatok as $rovat) {
									print "<option value='" . $rovat['slug'] . "' " . ($g_rovat == $rovat['slug'] ? "selected='selected'" : '') . ">" . $rovat['nev'] . "</option>";
									$alrovatok = $rovat['childs'];
									if( is_array( $alrovatok ) )
									foreach ($alrovatok as $alrovat) {
										print "<option value='" . $alrovat['slug'] . "' " . ($g_rovat == $alrovat['slug'] ? "selected='selected'" : '') . ">&nbsp;&nbsp;-&nbsp;" . $alrovat['nev'] . "</option>";
									}
								}
							}
						?>                  
					</select>
					<br />

					<label for='regio'>Régió</label>
					<select name='regio'>  
						<?php
							if (count($regiok) < 1) print "<option value='0'>Create a Régió first!</option>";
							else {
								print "<option value='mindegy'" . ($g_regio == $regio['slug'] ? "selected='selected'" : '') . ">Mindegy</option>";
								foreach ($regiok as $regio) {
									print "<option value='" . $regio['slug'] . "' " . ($g_regio == $regio['slug'] ? "selected='selected'" : '') . ">" . $regio['nev'] . "</option>";
									$alregiok = $regio['childs']; 
									if( is_array( $alregiok ) )
									foreach( $alregiok as $alregio ) {
										print "<option value='" . $alregio['slug'] . "' " . ($g_regio == $alregio['slug'] ? "selected='selected'" : '') . ">&nbsp;&nbsp;-&nbsp;" . $alregio['nev'] . "</option>";
									}
								}
							}
						?>                
					</select>
					<br />
					
					<label for='minar'>Min. ár</label>
					<input name='minar' type='text' value="<?php print $g_minar; ?>" />
					<br />
					
					<label for='maxar'>Max. ár</label>
					<input name='maxar' type='text' value="<?php print $g_maxar; ?>" />
					<br />

					<br />
										
					<label for="kereses">&nbsp;</label>
					<input name="kereses" type="submit" value="Keresés"/>				

				</form>
			</div>
			<img src='./images1/x5bgb.gif' width="21px" height="250px" class="fleft" alt='apróhirdetés' />
		</div>
		<div>
			<img src='./images1/x6a.gif' width="21px" height="34px" class="fleft" alt='apróhirdetés' /> 
			<img src='./images1/x6bg.gif' width="455px" height="34px" class="fleft" alt='apróhirdetés' /> 		
			<img src='./images1/x6b.gif' width="21px" height="34px" class="fleft" alt='apróhirdetés' />
		</div>
	</div>

	<br />

<?php } else { ?>
	
<?php if ($g_showtotal) { 
	$get = array_diff_key( $_GET, array( 'sorszam' => 0, 'showback' => 0, 'showform' => 0, 'showtotal' => 0));	
	$query_string = http_build_query( $get);		
?>
	<p class="ptop">
		<span><a href="hirdetesek.php?showform=1&amp;<?php print $query_string; ?>">&lt;&lt; Vissza a kereső formhoz</a></span>
		<span class="fright">Találatok száma: <?php print $tct; ?></span>	
	</p>
<?php } ?>

<?php if ($g_showback) { 	
	$get = array_diff_key( $_GET, array( 'sorszam' => 0, 'showback' => 0));	
	$query_string = http_build_query( $get);
	
?>
	<p class="ptop">
		<span><a href="hirdetesek.php?<?php print $query_string . '#' . $g_id;?>">&lt;&lt; Vissza a találati listához</a></span>	
	</p>

<?php } ?>

<?php if ($g_showtotal || $g_id > 0) print '<br />'; ?>

<?php if (count( $hirdetesek) < 1) { ?>

	<div class="form-panel">		
		<div>
			<img src='./images1/x3a2.gif' width="21px" height="42px" class="fleft" alt='apróhirdetés' /> 
			<div class="hptop">
				<p class="hpcaption">Információ</p>
				<p class="hptitle">Információ</p>
			</div>	
			<img src='./images1/x3b2.gif' width="21px" height="42px" class="fleft" alt='apróhirdetés' />
		</div>
		<div>
			<img src='./images1/x4a.gif' width="21px" height="13px" class="fleft" alt='apróhirdetés' /> 
			<img src='./images1/x4bg.gif' width="455px" height="13px" class="fleft" alt='apróhirdetés' /> 
			<img src='./images1/x4b.gif' width="21px" height="13px" class="fleft" alt='apróhirdetés' />
		</div>
		<div>
			<img src='./images1/x5bga.gif' width="21px" height="15px" class="fleft" alt='apróhirdetés' /> 
			<div class="hpbody" style="height:15px">			
				<p class="success">Nincs a kritériumnak megfelelő hirdetés.</p>			
			</div>
			<img src='./images1/x5bgb.gif' width="21px" height="15px" class="fleft" alt='apróhirdetés' />
		</div>
		<div>
			<img src='./images1/x6a.gif' width="21px" height="34px" class="fleft" alt='apróhirdetés' /> 
			<img src='./images1/x6bg.gif' width="455px" height="34px" class="fleft" alt='apróhirdetés' /> 		
			<img src='./images1/x6b.gif' width="21px" height="34px" class="fleft" alt='apróhirdetés' />
		</div>
	</div>
		
<?php }	?>

<?php foreach ($hirdetesek as $row) {

	if ($row['kep'] != '') {
		$kep   = $upload_path . '/' . str_replace('-','/',$row['feladas']) . '/kep/' . $row['kep']; 
		$thumb = $upload_path . '/' . str_replace('-','/',$row['feladas']) . '/thumb/thumb_' . $row['kep'];
		if (! file_exists ($kep)) {
			$kep   = 'http://hirdetek.net/kepek/'.$row['kep'];
			$thumb = 'http://hirdetek.net/thumbs1/thumb_'.$row['kep'];
		}
	} else {
		$kep   = '#';
		$thumb = './images1/nincskep.gif'; 
	}
					
?>
	<a name="<?php print $row['id']; ?>"></a>	
	<div class="form-panel">		
		<div>
			<img src='./images1/x3a2.gif' width="21px" height="42px" class="fleft" alt='apróhirdetés' /> 
			<div class="hptop">
				<div class="hplcdiv">
					<p class="hplcaption"><span>Apróhirdetés</span> sorszáma: <?php print $row['id']; ?></p>
					<p class="hprcaption">Feladó: <?php print $row['nev']; ?></p>
				</div>
				<div class="hprcdiv">
					<p class="hpltitle">Feladva: <?php print $row['feladas']; ?></p>
					<p class="hprtitle">Lejár: <?php print $row['lejarat']; ?></p>
				</div>
			</div>	
			<img src='./images1/x3b2.gif' width="21px" height="42px" class="fleft" alt='apróhirdetés' />
		</div>
		<div>
			<img src='./images1/x4a.gif' width="21px" height="13px" class="fleft" alt='apróhirdetés' /> 
			<img src='./images1/x4bg.gif' width="455px" height="13px" class="fleft" alt='apróhirdetés' /> 
			<img src='./images1/x4b.gif' width="21px" height="13px" class="fleft" alt='apróhirdetés' />
		</div>
		<div>
			<img src='./images1/x5bga.gif' width="21px" height="357px" class="fleft" alt='apróhirdetés' /> 
			<div class="hpbody" style="height:357px">							
				<div>
					<a class='hppic' href="#" onclick="return(makePopup(this,'<?php print str_replace( './upload', 'http://hirdetek.net/upload', $kep ); ?>'))">
						<img alt="<?php print $row['targy']; ?>" src="<?php print $thumb; ?>" />
					</a>
					<div class="hptext">
						<h1 class='targy'><?php print $row['targy']; ?></h1>						
						<textarea cols="35" rows="8" class='hirdszov' readonly="readonly"><?php print $row['szoveg']; ?></textarea>
					</div>				
				</div>
				<br />
				<hr />
				<div class="info1">
					<div class="info2">
						<table width="100%" cellspacing="3" border="0">
							<tr><td width="10%">Ár:</td><td width="90%"><?php print $row['ar']==''?'-':str_to_currency($row['ar']); ?></td></tr>
							<tr><td>Telefon:</td><td><?php print $row['telefon']==''?'-':$row['telefon']; ?></td></tr>
							<tr><td>Weblap:</td><td align="left"><?php if ($row['weblap']=='') print '-'; else { ?><a onclick="return(makePopup(this,'<?php print $row['weblap']; ?>'))">K a t t &nbsp; i d e !</a><?php } ?></td></tr>
						</table>
					</div>
					<div class="info2">
						<table width="100%" cellspacing="3" border="0">
							<tr><td width="10%">Régió:</td><td width="90%"><?php print substr($row['regio'],0,25); ?></td></tr>
							<?php if ($g_id < 1) { ?>
								<tr><td>Email:</td><td><a href="hirdetesek.php?sorszam=<?php print $row['id'] . '&amp;showback=1&amp;' . $_SERVER['QUERY_STRING']; ?>">K a t t &nbsp; i d e !</a></td></tr>
							<?php } else { ?>
								<tr><td>Email:</td><td><a onclick="return(makePopup(this,'<?php print 'mailto:'.$row['email']; ?>'))"><?php print $row['email'];?></a></td></tr>
							<?php } ?>
							<?php if ($row['szponzoralt'] > 0) { ?>
								<tr><td colspan="2"><a class="kiemeles" onclick='return tovabbiInfo ( this);' href='kiemeles.php'>KIEMELT HIRDETÉS</a></td></tr>
							<?php } else { ?>
								<tr><td colspan="2"><a class="kiemeles" onclick='return tovabbiInfo ( this);' href='kiemeles.php'>INGYEN HIRDETÉS KIEMELÉS</a></td></tr>
							<?php } ?>
						</table>
					</div>				
				</div>
				<br />
				<hr />
				<div class="info1">
					<div class="info2">
						<table width="100%" cellspacing="3" border="0">
							<tr><td><a href="hirdetesek.php?rovat=<?php print $row['forovat_slug']; ?>"><?php print $row['forovat']; ?> &gt;&gt;</a></td></tr>
							<tr><td><a href="hirdetesek.php?felado=<?php print $row['id'] . '&amp;' . $_SERVER['QUERY_STRING']; ?>">Feladó hirdetései &gt;&gt;</a></td></tr>
							<tr><td><a href="kedvencek.php?hirdetes-sorszama=<?php print $row['id'] . '&amp;' . $_SERVER['QUERY_STRING']; ?>">Kedvencekhez &gt;&gt;</a></td></tr>
						</table>
					</div>
					<div class="info2">
						<table width="100%" cellspacing="3" border="0">
							<tr><td><a href="<?php print $row['alrovat'] != '' ? 'hirdetesek.php?rovat=' . $row['alrovat_slug'] : ''; ?>"><?php print $row['alrovat'] != '' ? substr($row['alrovat'],0,35) . ' &gt;&gt;': '&nbsp;'; ?></a></td></tr>
							<tr><td><a onclick="return(makePopup(this,'http://hirdetek.net/hirdkuldes.php?id=<?php print $row['id']; ?>'))">Küldés emailben &gt;&gt;</a></td></tr>
							<tr><td><a onclick="return(makePopup(this,'http://hirdetek.net/nyomtatas.php?id=<?php print $row['id']; ?>'))">Nyomtatás &gt;&gt;</a></td></tr>
						</table>
					</div>				
				</div>
			</div>
			<img src='./images1/x5bgb.gif' width="21px" height="357px" class="fleft" alt='apróhirdetés' />
		</div>
		<div>
			<img src='./images1/x6a.gif' width="21px" height="34px" class="fleft" alt='apróhirdetés' /> 
			<img src='./images1/x6bg.gif' width="455px" height="34px" class="fleft" alt='apróhirdetés' /> 		
			<img src='./images1/x6b.gif' width="21px" height="34px" class="fleft" alt='apróhirdetés' />
		</div>
	</div>
	
	<?php if (User::is_logged_in() && (User::get_email() == $row['email'] || User::get_id() == 3)) { ?>
		
		<br />
		
		<div class="usertools">	
			<table cellspacing="3" border="0">
				<tr>
					<td><a href="hirdetes-modositas.php?<?php print 'sorszam='.$row['id'].'&amp;kod='.$row['aktivkod']; ?>">Módosítás</a></td>
					<td><a href="hirdetes-hosszabbitas.php?<?php print 'sorszam='.$row['id'].'&amp;kod='.$row['aktivkod']; ?>">Hosszabítás</a></td>
					<td><a onclick="return confirm('Biztosan törli?')" href="hirdetes-torles.php?<?php print 'sorszam='.$row['id'].'&amp;kod='.$row['aktivkod']; ?>">Törlés</a></td>
					<?php if( User::get_id() == 3 ) { ?>
						<td><a href="admin-kiemeles.php?<?php print 'sorszam='.$row['id'].'&amp;kod='.$row['aktivkod']; ?>">Kiemelés</a></td>
						<td><a href="admin-beemeles.php?<?php print 'sorszam='.$row['id'].'&amp;kod='.$row['aktivkod']; ?>">Beemelés</a></td>
						<td><a onclick="return confirm('Biztosan kitiltja?')" href="admin-ban.php?<?php print 'sorszam='.$row['id'].'&amp;kod='.$row['aktivkod']; ?>">Kitiltás</a></td>
					<?php } ?>
				</tr>
			</table>
			<hr />		
		</div>		
		<br />
		
	<?php } else { ?>
	
		<br />
		<br />
	
	<?php } ?>
	
<?php } ?>
 
<br />

<div id="pager"><?php echo $pager->links; ?></div>

<?php } ?>

<br />

</div>

<?php include ("right.php"); ?>

<?php include ("footer.php"); ?>
